package com.sishu.websocket4.controller;

import cn.dev33.satoken.util.SaResult;
import com.sishu.websocket4.pojo.Tiku;
import com.sishu.websocket4.service.Tikuservice;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/tiku/")
@CrossOrigin(origins = "*")
public class TikuController {


    @Resource
    private Tikuservice tikuservice;


    //随机获取一道题库中的题目
    @RequestMapping("getramtiku")
    public SaResult getramtiku(){
      return  tikuservice.getRandomTiku();
    }

    //回答随机抽取的题目
    @RequestMapping("answerramtiku")
    public SaResult answerramtiku(@Param("questionId")String questionId,@Param("answer")String answer){
        return  tikuservice.Tikuanswer(questionId,answer);
    }
    //增加题目
    @RequestMapping("insert")
    public SaResult inserttiku(@RequestBody List<Tiku> tikus){
        //循环打印传过来的题目是不是在数据库中
        boolean flag = true;//默认是没有在数据库中
        SaResult selecttiku =SaResult.ok();
        for (Tiku tiku : tikus) {
             selecttiku = tikuservice.selectTiku(tiku);
            System.out.println("selecttiku = " + selecttiku);

            if (selecttiku.getCode()==500){
                flag = false;//题库中有此题目
                return selecttiku;
            }
        }

        if (selecttiku.getCode()==200){
            //   插入题目
            System.out.println("tiku =" + tikus);
            return tikuservice.insertTiku(tikus);
        }

            return selecttiku;
    }



}


